package com.example.rescuer.zyfypt_926wyr.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.rescuer.zyfypt_926wyr.*;
import com.example.rescuer.zyfypt_926wyr.bean.LoginBean;
import com.example.rescuer.zyfypt_926wyr.iface.LoginListener;
import com.example.rescuer.zyfypt_926wyr.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText etuser, etpass;
    private String username = "", password = "", sessionID = "";
    private Switch sw;
    private int userid;
    //存储简单信息，并且以xml格式文件形式存储
    private SharedPreferences sp;

    //返回登录结果：失败/成功
    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            sessionID = loginBean.getSessionid();
            System.out.println("----sessionID=" + sessionID);
            if (sessionID != null) {
                saveSP();
                //打开主窗口
                Toast.makeText(LoginActivity.this, "登录成功--sessionID=" + sessionID, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();

            } else
                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();

        }
    };
    @Override
    //savedInstanceState保存当前activity的状态信息
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);//启动登录
        init();

        //键值对
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();
}
    //读取sp信息
    private void readSP() {
        //存取键值对
        String strname=sp.getString("name",null);
        String strpass=sp.getString("pass",null);
        Boolean remember=sp.getBoolean("remember",false);
        if(remember)
        {
            etuser.setText(strname);
            etpass.setText(strpass);
        }
        sw.setChecked(remember);

    }
    //保存信息
    private  void saveSP() {
        //获取一个对象
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("name",etuser.getText().toString());//从编辑框获取
        editor.putString("pass",etpass.getText().toString());
        editor.putBoolean("remember",sw.isChecked());
        editor.putString("sessionID",sessionID);
        editor.putInt("userid",userid);
        //将添加的数据提交
        editor.commit();

    }

    private void init() {
        //onClicklistener是一个接口,不能实例化,这就是一个匿名内部类,这个类实现onClickListener
        //然后被new 了,无形中传了一个对象进去,这个对象给了button/TextView中的mOnClicklistener,就是这家伙调用了onClick方法
//当然要是Activity实现了这个接口,就可以setonClickListener(this)
//this是当前类的一个对象
//传进去一个Activity对象给monclickListener,就是一个接口引用指向Activity对象,不过接口引用只能调用自己的方法
        findViewById(R.id.btnlogin).setOnClickListener(this);
        findViewById(R.id.btnregister).setOnClickListener(this);
        etuser = (EditText) findViewById(R.id.editText);
        etpass = (EditText) findViewById(R.id.editText2);
        sw=findViewById(R.id.switch1);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnlogin://登录
                username = etuser.getText().toString();
                password = etpass.getText().toString();
                //点击登录按钮响应登录结果
                LoginModel loginModel = new LoginModel();
                loginModel.getLoginResult(username, password, loginListener);

                break;
            case R.id.btnregister://注册
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                Toast.makeText(this, "注册", Toast.LENGTH_SHORT).show();
                break;

        }
    }
}


